using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using vs.Core;
using vs.Authentication;

namespace Forum
{
    public partial class forum_edit : System.Web.UI.UserControl
    {
        private bool validating()
        {
            try
            {
                int i = Convert.ToInt32(txtOrderIndex.Text);
            }
            catch
            {
                lblMessage.Text = "You have to enter a number";
                lblMessage.Visible = true;
                return false;
            }

            return true;
        }

        private void bind_category()
        {
            string strSQL = "SELECT CategoryName, CategoryID FROM FORUM_Category ORDER BY OrderIndex";
            DataTable dt = DataFactory.SelectTable(strSQL);

            ddlCat.DataSource = dt;
            ddlCat.DataTextField = "CategoryName";
            ddlCat.DataValueField = "CategoryID";
            ddlCat.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    bind_category();

                    ForumController db = new ForumController();
                    ForumInfo fr = db.Load(Request.QueryString["ID"]);

                    ddlCat.SelectedValue = fr.CategoryID;
                    lblForumID.Text = fr.ForumID;
                    txtDescription.Text = fr.Description;
                    txtName.Text = fr.ForumName;
                    txtOrderIndex.Text = fr.OrderIndex.ToString();

                    btnDelete.Attributes.Add("onclick", "if(!confirm('Ban co chac chan xoa?')) {return false;};");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                lblMessage.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                ForumInfo fr = new ForumInfo();
                fr.CategoryID = ddlCat.SelectedValue;
                fr.Description = txtDescription.Text;
                fr.ForumID = lblForumID.Text;
                fr.ForumName = txtName.Text;
                fr.OrderIndex = Convert.ToInt32(txtOrderIndex.Text);
                ForumController db = new ForumController();

                if (!validating()) return;
                db.Update(fr);

                btnCancel_Click(null, new EventArgs());
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                lblMessage.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ForumController db = new ForumController();
                db.Delete(Request.QueryString["ID"]);

                btnCancel_Click(null, new EventArgs());
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                lblMessage.Visible = true;
            }          
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(UrlProvider.AdminUrl("fr_forum", "admin"));
        }
    }
}